/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.POJO_ChuyenTau;
import POJO.POJO_DoanTau;
import POJO.POJO_TuyenTau;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Master
 */
public class DAO_ChuyenTau {
    DataProvider data;
    
    public List<POJO_ChuyenTau> LoadList()throws SQLException {
        data = new DataProvider();
        data.Open();
        
        String query = "SELECT * FROM chuyen_tau";
        
        List<POJO_ChuyenTau> lst = new ArrayList<>();
        ResultSet rs = data.ExecuteQuery(query);
        
        while (rs.next()) {
            POJO_ChuyenTau ct = new POJO_ChuyenTau();
            //POJO_TuyenTau tt = new POJO_TuyenTau();
            //POJO_DoanTau dt = new POJO_DoanTau();

            ct.setMACHUYEN(rs.getString("MaChuyen"));
            ct.setMATUYEN(rs.getString("MaTuyen"));
            ct.setMADT(rs.getString("MaDT"));
            ct.setTHOIGIANXP(rs.getDate("THOIGIANXP"));

            lst.add(ct);
        }
        
        return lst;
    } 
    
    
    public int xoaChuyenTau(POJO_ChuyenTau ct) {
        
            data = new DataProvider();
            data.Open();
            
            String sql = String.format("DELETE FROM `chuyen_tau` WHERE MaChuyen = '%s'", ct.getMACHUYEN());
            
            return data.ExeCuteUpdate(sql);
    }
    
    public int themChuyenTau(POJO_ChuyenTau ct) {
        
            data = new DataProvider();
            data.Open();
            
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String sql = String.format("INSERT INTO `chuyen_tau`(`MaChuyen`, `MaTuyen`, `MaDT`, `ThoiGianXP`) VALUES ('%s','%s','%s','%s')",
                    ct.getMACHUYEN(),
                    ct.getMATUYEN(),
                    ct.getMADT(),
                    ct.getTHOIGIANXP()
            );                               
            return data.ExeCuteUpdate(sql);
    } 
    
    public int capNhatChuyenTau(POJO_ChuyenTau ct)  {
        
            data = new DataProvider();
            data.Open();    
        
            String sql = String.format("UPDATE `chuyen_tau` SET `MaChuyen`='%s',`MaTuyen`='%s',`MaDT`='%s',`ThoiGianXP`='%s' WHERE MaChuyen = '%s'",                    
                    ct.getMACHUYEN(),
                    ct.getMATUYEN(),
                    ct.getMADT(),
                    ct.getTHOIGIANXP(),
                    ct.getMACHUYEN()
            );
                    
            return data.ExeCuteUpdate(sql);
    }
}
